#ifndef __ENGINE_VERSION_TAG_included
#define __ENGINE_VERSION_TAG_included

#include <string>
#include <boost/lexical_cast.hpp>
#include "package.h"

using namespace std;

namespace Engine {

class CVersionTag {

public :
	
	int mSessionId;
	int mDeviceId;
	string mTagName;
	int mVersion;
	string mInfo;

	CVersionTag(int session, int dev, const string& name, int ver) :
		mSessionId(session), mDeviceId(dev), mTagName(name), mVersion(ver)
	{ }

	CVersionTag(CPackage::JsonValue& o) : mDeviceId(-1), mSessionId(-1) {
		
		CPackage::JsonValue& v1 = o["dev"];
		CPackage::JsonValue& v2 = o["name"];
		CPackage::JsonValue& v3 = o["head"];
		CPackage::JsonValue& v4 = o["session"];
		CPackage::JsonValue& v5 = o["info"];

		if(!v2.IsString() || !v3.IsInt()) return;

		try {
			if(v4.IsString()) mSessionId = boost::lexical_cast<int>(v4.GetString());
			if(v1.IsString()) mDeviceId = boost::lexical_cast<int>(v1.GetString());
			if(v5.IsString()) mInfo = v5.GetString();
			mTagName = v2.GetString();
			mVersion = v3.GetInt();
		}
		catch(std::exception&) {
		}
	}

	void WriteJson(CPackage::JsonWriter& writer) {

		writer.StartObject();
		if(mSessionId > 0) writer.String("session").String(boost::lexical_cast<string>(mSessionId).c_str());
		if(mDeviceId > 0) writer.String("dev").String(boost::lexical_cast<string>(mDeviceId).c_str());
		writer.String("name").String(mTagName.c_str());
		writer.String("head").Int(mVersion);
		if(mInfo.length() > 0) writer.String("info").String(mInfo.c_str());
		writer.EndObject();

	}
};

}

#endif //__ENGINE_VERSION_TAG_included